﻿#region

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using BusinessObjects;

#endregion

namespace DataAccessLayer
{
    public static class AdministratorDAL
    {
        private static readonly string ConnectionString =
            ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

        /// <summary>
        ///     Register a administrator
        /// </summary>
        /// <param name="admin">Represent's the administrator</param>
        public static void RegisterAdmin(Administrator admin)
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                var command = new SqlCommand("spAddAdministrator", connection)
                {
                    CommandType = CommandType.StoredProcedure
                };

                command.Parameters.AddWithValue("@StaffId", admin.StaffId);
                command.Parameters.AddWithValue("@Username", admin.UserCredentials.Username);
                command.Parameters.AddWithValue("@Password", admin.UserCredentials.Password);
                command.Parameters.AddWithValue("@Role", admin.Role);

                connection.Open();
                command.ExecuteNonQuery();
            }
        }

        /// <summary>
        ///     Get All administrators
        /// </summary>
        /// <returns>All administrators</returns>
        public static List<Administrator> GetAllAdministrators()
        {
            var administrators = new List<Administrator>();
            using (var connection = new SqlConnection(ConnectionString))
            {
                var command = new SqlCommand("spGetAllAdministrators", connection)
                {
                    CommandType = CommandType.StoredProcedure
                };

                connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var administrator = new Administrator
                        {
                            StaffId = Convert.ToInt32(reader["StaffId"]),
                            Role = reader["Role"].ToString()
                        };

                        var userCredentials = new User
                        {
                            Username = reader["Username"].ToString()
                        };

                        administrator.UserCredentials = userCredentials;

                        administrators.Add(administrator);
                    }
                }
            }
            return administrators;
        }

        /// <summary>
        ///     Get administrator by staff Id
        /// </summary>
        /// <param name="staffId">Represent's the staff id</param>
        /// <returns>Administrator</returns>
        public static Administrator GetAdministrator(int staffId)
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                var command = new SqlCommand("spGetAdminByStaffId", connection)
                {
                    CommandType = CommandType.StoredProcedure
                };

                command.Parameters.AddWithValue("@StaffId", staffId);

                connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        var administrator = new Administrator
                        {
                            StaffId = Convert.ToInt32(reader["StaffId"]),
                            Role = reader["Role"].ToString()
                        };

                        var userCredentials = new User
                        {
                            Username = reader["Username"].ToString()
                        };

                        administrator.UserCredentials = userCredentials;

                        return administrator;
                    }
                }
            }
            return null;
        }

        /// <summary>
        ///     Get administrator by username
        /// </summary>
        /// <param name="username">Represent's the administrator's username</param>
        /// <returns>Administrator</returns>
        public static Administrator GetAdministrator(string username)
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                var command = new SqlCommand("spGetAdministratorByUsername", connection)
                {
                    CommandType = CommandType.StoredProcedure
                };

                command.Parameters.AddWithValue("@Username", username);

                connection.Open();
                using (var reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        var administrator = new Administrator
                        {
                            StaffId = Convert.ToInt32(reader["StaffId"]),
                            Role = reader["Role"].ToString()
                        };

                        var userCredentials = new User
                        {
                            Username = reader["Username"].ToString()
                        };

                        administrator.UserCredentials = userCredentials;

                        return administrator;
                    }
                }
            }
            return null;
        }
    }
}